package com.ellricky.baselib.ui.cla

import android.view.View
import android.widget.AdapterView
import com.andr.common.http.callback.BaseCallback
import com.andr.common.http.callback.JOCallback
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.http.Api
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEBaseCallback
import com.ellricky.baselib.util.NewEntityCallback
import kotlinx.android.synthetic.main.base_activity_person_add.*
import org.json.JSONObject

abstract class PersonAddActivity : BaseToolBarActivity<Void, BasePresenter<Void>>(),
    AdapterView.OnItemClickListener {

    private lateinit var classId: String // 班级id
    private lateinit var courseListId: String

    // A类班级，第一排添加的老师和学生 sortNo =1 ，
    //          第二排 sortNo =2 ；
    // B类班级，添加老师和学生，sortNo =1
    private lateinit var sortNo: String

    override fun getContentLayout() = R.layout.base_activity_person_add

    override fun initUIView() {
        super.initUIView()

        classId = intent.getStringExtra("classId") ?: ""
        sortNo = intent.getStringExtra("sortNo") ?: ""
        courseListId = intent.getStringExtra("course_list_id") ?: ""
        tvLabel.text = label()

        ibSearch.setOnClickListener(this)
        btnSubmit.setOnClickListener(this)

        queryData()
    }


    override fun onClickView(v: View?) {
        super.onClickView(v)
        when (v) {
            ibSearch -> {
                val search = etSearch.text.toString().trim()
                queryData(search)
            }
            btnSubmit -> addPerson()
        }
    }


    private fun queryData(search: String = "") {
        val url = EAppUtil.getApiHost() + getUrlPath()
        val paramMap = hashMapOf<String, String>()
        paramMap["search_key"] = search
        paramMap["page"] = "1"
        paramMap["page_size"] = "100"
        queryPersonParam(paramMap)
        HttpUtils.getInstance().postFrom(url, paramMap, getResponseCallback())
    }

    abstract fun getUrlPath(): String

    abstract fun getResponseCallback(): NewEBaseCallback

    private fun addPerson() {
        val url = EAppUtil.getApiHost()
        val paramMap = hashMapOf<String, String>()
        paramMap["class_grade_id"] = classId
        paramMap["sort_no"] = sortNo
        paramMap["course_list_id"] = courseListId
        paramMap["eid"] = LocalCacheUtil.getUid()
        addPersonParam(paramMap)

        HttpUtils.getInstance().postFrom(url, paramMap, object :NewEntityCallback<Any>(this){
            override fun onSuccess(var1: Any?, var2: String?) {
                setResult(BaseConstant.RESULT_CODE_ADD)
                finish()
            }
        })
    }

    abstract fun label(): CharSequence?

    abstract fun addPersonParam(paramMap: HashMap<String, String>)

    abstract fun queryPersonParam(paramMap: HashMap<String, String>)
}